package yl.hs.bmipfull.service.production.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.production.IProductHardwareDao;
import yl.hs.bmipfull.daos.production.IProductHardwareInventoryDao;
import yl.hs.bmipfull.pojo.production.ProductHardwareEntity;
import yl.hs.bmipfull.pojo.production.ProductHardwareInventoryEntity;
import yl.hs.bmipfull.service.production.IProductHardwareInventoryService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:  五金库存服务层接口实现<br/>
 * 创建时间: 2023年12月01日 13时53分41秒 <br/>
 *
 * @author 吴高丰
 */
@Service
public class ProductHardwareInventoryServiceImpl extends ProductionBaseService implements IProductHardwareInventoryService {

    @Autowired
    IProductHardwareInventoryDao dao;
    @Autowired
    IProductHardwareDao hardwareDao;
    @Override
    public List<ProductHardwareInventoryEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(ProductHardwareInventoryEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }

    @Override
    public PageResult<ProductHardwareInventoryEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ProductHardwareInventoryEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        PageResult<ProductHardwareInventoryEntity> result = new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }


    @Override
    public ProductHardwareInventoryEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveproduct_hardware_inventory(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        ProductHardwareInventoryEntity entity = null;
        if (Tl.isEmpty(sn)) {
            //执行保存
            entity = new ProductHardwareInventoryEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ProductHardwareInventoryEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                var hardware = hardwareDao.findBySN(entity.getHardwareSN());
                if(hardware!=null){
                    hardware.setQuantity(entity.getNewQuantity());
                    var ss = hardwareDao.update(hardware);
                    if(ss>0){
                        writeLog(ActionType.CREATE, hardware.getSn(), "创建盘库记录，库存由"+entity.getOldQuantity()+"变为"+entity.getNewQuantity(), Tl.toJson(entity), ProductHardwareEntity.class);
                    }
                }
                writeLog(ActionType.CREATE, entity.getSn(), "插入五金库存信息", Tl.toJson(entity), ProductHardwareInventoryEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新五金库存信息", Tl.toJson(entity), ProductHardwareInventoryEntity.class);
            }
        }

        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(String sn) {
        var sns = sn.split(",");
        Integer result = dao.changeStateBySN(sns, DataState.DELETED.code);
        if (result > 0) {
            writeLog(ActionType.DELETE, sns, ProductHardwareInventoryEntity.class);
        }
        return result;
    }
}